import numpy as np
import torch
import torch.nn as nn
import matplotlib.pyplot as plt
from torch.autograd import Variable

input_size = 1
output_size = 1
learning_rate = 0.001
x_train = np.array(
    [[2.3], [4.4], [3.7], [6.1], [7.3], [2.1], [5.6], [7.7], [8.7], [4.1], [6.7], [6.1], [7.5], [2.1], [7.2], [5.6],
     [5.7], [7.7], [3.1]], dtype=np.float32)
y_train = np.array(
    [[3.7], [4.76], [4.], [7.1], [8.6], [3.5], [5.4], [7.6], [7.9], [5.3], [7.3], [7.5], [8.5], [3.2], [8.7], [6.4],
     [6.6], [7.9], [5.3]], dtype=np.float32)


# plt.figure()
# plt.scatter(x_train, y_train)
# plt.xlabel('x_train')
# plt.ylabel('y_train')
# plt.show()

# 定义模型
class LinearRegression(nn.Module):
    def __init__(self, input_size, output_size):
        super(LinearRegression, self).__init__()
        self.linear = nn.Linear(input_size, output_size)

    def forward(self, x):
        out = self.linear(x)
        return out


model = LinearRegression(1, 1)

# 定义criterion误差函数----------->MSELoss最小二乘loss
criterion = nn.MSELoss()

# 优化函数----------------------->随机梯度下降
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)

num_epoches = 1000

for epoch in range(num_epoches):
    # 前向传播--------------------->
    inputs = Variable(torch.from_numpy(x_train))
    targets = Variable(torch.from_numpy(y_train))

    outputs = model(inputs)

    # 计算loss--------------------->
    loss = criterion(outputs, targets)

    # pytorch的一个特点是每一步都是独立功能的操作
    # 反向传播的时候需要将梯度归零
    # 如若不显示的进行optimizer.zero_grad()这一步操作，
    # backward()的时候就会累加梯度，也就有了各位答主所说到的梯度累加这种trick。
    optimizer.zero_grad()

    # tensor不能反向传播，variable可以反向传播
    loss.backward()

    # 更新权重参数
    optimizer.step()

    if (epoch + 1) % 50 == 0:
        # print(loss.data)
        print('Epoch[%d/%d],Loss: %.4f' % (epoch + 1, num_epoches, loss.item()))


model.eval()
predicted = model(Variable(torch.from_numpy(x_train)))
predicted = predicted.data.numpy()
plt.plot(x_train, y_train, 'ro')
print(predicted)
plt.plot(x_train, predicted, label='predict')
plt.legend()
plt.show()
